﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using IFactory;


public partial class WebCommon_VerifyCircrtForTotalPlan : System.Web.UI.Page
{
    QuarterPlanofManager dllQuarterPlan = new QuarterPlanofManager();
    IBussinessLogic dllMonthPlan = new MonthPlanManager();
    IBussinessLogic dllWeekPlan = new WeekPlanofManager();
    VerifyCircuitManager dllVerifyCircuit = new VerifyCircuitManager();
    UserInfoManager dllUserInfoManager = new UserInfoManager();
    SystemLog dllSystemLog = new SystemLog();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getVerifyCircruit();
            getSystemLog();
        }
    }

    /// <summary>
    /// 得到指定计划的流程和对应的日志
    /// </summary>
    void getVerifyCircruit()
    {
        int _planId = Utility.HttpHelp.getParamsInt("PlanId");
        int _planType = Utility.HttpHelp.getParamsInt("PlanType");
        object[] _value = new object[1];
        _value[0] = _planId;
        DataTable _planTable = new DataTable();
        DataTable _systemLog = new DataTable();       
        if (_planType == 4)//总表计划类型
        {
            TotalPlanMgr dllTotalPlanMgr = new TotalPlanMgr();
            _planTable = dllTotalPlanMgr.get(TotalPlanMgr.field.TotalPlanId.ToString(), _value);
            object[] _valueTotal = new object[2];
            _valueTotal[0] = _planId;
            _valueTotal[1] = _planType;
            _systemLog = dllSystemLog.getWeekPlanTrueLog(_valueTotal);
        }

        bindTable(_planTable, _systemLog);

    }


    /// <summary>
    /// 得到该计划对应的日志
    /// </summary>
    void getSystemLog()
    {
        object[] _value = new object[2];
        _value[0] = Utility.HttpHelp.getParamsInt("PlanId");
        _value[1] = Utility.HttpHelp.getParamsInt("PlanType");
        DataTable _systemLog = dllSystemLog.getSystemLog(_value);
        this.gdvVircuit.DataSource = _systemLog;
        this.gdvVircuit.DataBind();
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="_planTable"></param>
    void bindTable(DataTable _planTable, DataTable _systemLog)
    {
        //创建图像行
        TableRow _trVerifyImage = new TableRow();
        //创建姓名行
        TableRow _trVerifyPeople = new TableRow();
        //创建时间行
        TableRow trTime = new TableRow();

        #region 绑定发起人
        //根据计划id查询发起人
        string _sponsorPeople = "";
        object[] _sponsor = new object[1];
        _sponsor[0] = _planTable.Rows[0]["Sponsor"].ToString();
        DataTable _userinfo = dllUserInfoManager.getSponsor(_sponsor);
        if (_userinfo.Rows.Count > 0)
        {
            _sponsorPeople = _userinfo.Rows[0]["RealName"].ToString();
        }
        else
        {
            Utility.Error.RedirectErrorPage("数据读取错误，请重试！");
        }
        //定义发起人图像
        TableCell _tcSponsorPeopleImage = new TableCell();
        Image _imgSponsorPeople = new Image();
        //定义发起人时间
        TableCell _tcSponsorTime = new TableCell();
        Label _lblSponsorTime = new Label();
        if (_systemLog.Rows.Count > 0)
        {
            _lblSponsorTime.Text = _systemLog.Rows[0]["TimeDate"].ToString().Replace(" ", "<br>");
        }
        else
        {
            _lblSponsorTime.Text = "";
        }
        if (_planTable.Rows[0]["PlanSendState"].ToString() == "0")
        {
            _imgSponsorPeople.ImageUrl = "../images/verifyPeople.gif";
        }
        else
        {
            _imgSponsorPeople.ImageUrl = "../images/verifyPeople1.gif";
        }
        //绑定发起人图像到table
        _tcSponsorPeopleImage.Controls.Add(_imgSponsorPeople);
        _trVerifyImage.Cells.Add(_tcSponsorPeopleImage);
        //绑定发起人时间到table
        _tcSponsorTime.Controls.Add(_lblSponsorTime);
        trTime.Controls.Add(_tcSponsorTime);
        //创建箭头单元格
        _tcSponsorPeopleImage = new TableCell();
        Label _lbl = new Label();
        _lbl.Text = "—>";
        _tcSponsorPeopleImage.Controls.Add(_lbl);
        _trVerifyImage.Cells.Add(_tcSponsorPeopleImage);

        //绑定发起人名称到ｔａｂｌｅ
        TableCell _tcSponsorPeople = new TableCell();
        Label _lblSponsorPeople = new Label();
        _lblSponsorPeople.Text = _sponsorPeople;
        _tcSponsorPeople.Controls.Add(_lblSponsorPeople);
        _trVerifyPeople.Cells.Add(_tcSponsorPeople);

        //创建发起人后箭头单元格
        _tcSponsorPeople = new TableCell();
        _lbl = new Label();
        _lbl.Text = "—>";
        _lbl.Visible = false;
        _tcSponsorPeople.Controls.Add(_lbl);
        _trVerifyPeople.Cells.Add(_tcSponsorPeople);
        //创建发起人时间后空格
        _tcSponsorTime = new TableCell();
        trTime.Controls.Add(_tcSponsorTime);

        #endregion

        

        #region  绑定流程人员
        //根据计划id 查询流程id并得到流程数
        int _verifyCircuitId = Convert.ToInt32(_planTable.Rows[0]["VerifyCircuitId"].ToString());
        object[] _value = new object[1];
        _value[0] = _verifyCircuitId;
        DataTable _verifyCircuit = dllVerifyCircuit.get(VerifyCircuitManager.field.VerifyCircuitId.ToString(), _value);


        for (int i = 0; i < _verifyCircuit.Rows.Count; i++)
        {
            //创建图像单元格
            TableCell _tcVerifyImage = new TableCell();
            Image _imgVerifyPeople = new Image();
            //属于审核流程中
            if (_planTable.Rows[0]["PlanSendState"].ToString() == "2")
            {
                object[] _verifyValue = new object[2];
                object[] _planValue = new object[2];
                _verifyValue[0] = _verifyCircuit.Rows[i]["VerifyPeople"].ToString();
                _verifyValue[1] = Convert.ToInt32(_verifyCircuit.Rows[i]["VerifyCircuitId"].ToString());
                _planValue[0] = _planTable.Rows[0]["VerifyPeople"].ToString();
                _planValue[1] = Convert.ToInt32(_planTable.Rows[0]["VerifyCircuitId"].ToString());
                DataTable _verify = dllVerifyCircuit.get(VerifyCircuitManager.field.VerifyPeople.ToString(), _verifyValue);
                DataTable _plan = dllVerifyCircuit.get(VerifyCircuitManager.field.VerifyPeople.ToString(), _planValue);
                if (_verify.Rows.Count > 0 && _plan.Rows.Count > 0)
                {
                    if (Convert.ToInt32(_verify.Rows[0]["Id"].ToString()) >= Convert.ToInt32(_plan.Rows[0]["Id"].ToString()))
                    {
                        _imgVerifyPeople.ImageUrl = "../images/verifyPeople.gif";
                    }
                    else
                    {
                        _imgVerifyPeople.ImageUrl = "../images/verifyPeople1.gif";
                    }
                }
            }

            //属于全部流程结束后
            if (Convert.ToInt32(_planTable.Rows[0]["PlanSendState"].ToString()) >= 3)
            {
                _imgVerifyPeople.ImageUrl = "../images/verifyPeople1.gif";
            }
            else
            {
                _imgVerifyPeople.ImageUrl = "../images/verifyPeople.gif";
            }

            _tcVerifyImage.Controls.Add(_imgVerifyPeople);
            _trVerifyImage.Cells.Add(_tcVerifyImage);


            //创建审核人名称单元格
            TableCell _tcVerifyPeople = new TableCell();
            Label _lblVerifyPeople = new Label();
            _lblVerifyPeople.Text = _verifyCircuit.Rows[i]["VerifyPeopleRealName"].ToString();
            _tcVerifyPeople.Controls.Add(_lblVerifyPeople);
            _trVerifyPeople.Cells.Add(_tcVerifyPeople);



            //创建审核人时间空单元格
            TableCell _tcVerifyTime = new TableCell();
            trTime.Cells.Add(_tcVerifyTime);
            //创建审核人时间单元格
            _tcVerifyTime = new TableCell();
            Label _lblVerifyTime = new Label();
            if (_systemLog.Rows.Count > 2)
            {
                _lblVerifyTime.Text = _systemLog.Rows[i + 2]["TimeDate"].ToString().Replace(" ", "<br>");
            }
            else
            {
                _lblVerifyTime.Text = "";
            }
            _tcVerifyTime.Controls.Add(_lblVerifyTime);
            trTime.Cells.Add(_tcVerifyTime);

            //创建箭头单元格
            _tcVerifyImage = new TableCell();
            Label _lblArrow = new Label();
            _lblArrow.Text = "";
            _tcVerifyImage.Controls.Add(_lblArrow);
            _trVerifyImage.Cells.Add(_tcVerifyImage);

            ////创建空白单元格
            _tcVerifyPeople = new TableCell();
            _lblVerifyPeople = new Label();
            _lblVerifyPeople.Text = "";
            _tcVerifyPeople.Controls.Add(_lblVerifyPeople);
            _trVerifyPeople.Cells.Add(_tcVerifyPeople);

        }
        this.tableVerify.Rows.Add(_trVerifyImage);
        this.tableVerify.Rows.Add(_trVerifyPeople);
        this.tableVerify.Rows.Add(trTime);
        for (int j = 0; j < this.tableVerify.Rows.Count; j++)
        {
            this.tableVerify.Rows[j].HorizontalAlign = HorizontalAlign.Center;
        }
        #endregion


     

        //if (_trVerifyImage.Cells.Count == 2)
        //{
        //    _trVerifyImage.Cells.RemoveAt(_trVerifyImage.Cells.Count - 1);
        //}

    }
}
